package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.redis.service.IStatisticsVORedisService;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
/**
* 攻略评论服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStatisticsVORedisService statisticsVORedisService;



    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
        // 评论一次，执行一次评论数累加
        statisticsVORedisService.addReplynum(strategyComment.getStrategyId(), 1);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.DESC, "createTime");
        return DBHelper.query(template,pageable, query,StrategyComment.class);
    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        if(StringUtils.hasLength(strategyComment.getId())){
            //update
            this.update(strategyComment);
        }else{
            //save
            this.save(strategyComment);
        }
    }

    @Override
    public void commentThumb(String cid, UserInfo userInfo) {
        StrategyComment comment = this.get(cid);
        // 获取已点赞的用户数组
        List<String> thumbs = comment.getThumbuplist();
        //如果当前用户id已在该集合中，表名之前已经点赞，现在的请求是取消点赞
        if (thumbs.contains(userInfo.getId())) {
            // 该条评论的点赞数-1
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            // 移除当前的集合的用户id
            thumbs.remove(userInfo.getId());
        }else {
            //如果当前用户id不在该集合中，表名现在的请求是点赞
            // 该条评论的点赞数+1
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            // 添加当前用户id到集合中
            thumbs.add(userInfo.getId());
        }
        // 对应的状态设置回去
        comment.setThumbuplist(thumbs);
        // 更新数据库状态
        this.update(comment);
    }
}
